package com.hx.zhg.util;

import java.math.BigDecimal;

/**
 * Created by zhenghegong on 2018/1/7.
 */
public class MathUtilExt {

    public static int roundInt(float val) {
        return Math.round(val);
    }

    public static float round(float val) {
        int num = 1;
        return round(val, num);
    }

    public static float round(float val, int num) {
        BigDecimal weightBg = new BigDecimal(val);
        return weightBg.setScale(num, BigDecimal.ROUND_HALF_UP).floatValue();
    }

    public static float round(String val) {
        int num = 1;
        return round(val, num);
    }

    public static float round(String val, int num) {
        BigDecimal weightBg = new BigDecimal(val);
        return weightBg.setScale(num, BigDecimal.ROUND_HALF_UP).floatValue();
    }

    public static boolean isPercentage(float val) {
        if (val >= 1F && val <= 100F) {
            return true;
        }
        return false;
    }

    public static boolean isPercentage(int val) {
        if (val >= 1 && val <= 100) {
            return true;
        }
        return false;
    }

    public static boolean isPercentage(double val) {
        if (val >= 1 && val <= 100) {
            return true;
        }
        return false;
    }

    //保留指定小数位
    public static double round(double val, int num) {
        BigDecimal weightBg = new BigDecimal(val);
        return weightBg.setScale(num, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

}
